package com.liuyu.mall2.controller.admin;

import com.liuyu.mall2.entity.Manager;
import com.liuyu.mall2.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘宇
 * @classname ManagerController
 * @time 2025/4/8
 * @project mall2
 */
@Controller
@RequestMapping("/admin/manager")
public class ManagerController {
    @Autowired
    private ManagerService managerService;
    @GetMapping("/add")
public String add(){
    return "manager_add";
}
@PostMapping("/add")
    public String add(Manager manager){
        manager.setStatus("y");
        managerService.add(manager);
        //重定向到管理员列表页面
        return "redirect:/admin/manager/list";
    }
    @GetMapping("/list")
    public String list(Model model){
        List<Manager> managers = managerService.list();
        model.addAttribute("managers",managers);
        return "manager_list";
    }
    @GetMapping("/update/{id}")
    public String update(@PathVariable int id,Model model){
        Manager manager = managerService.getById(id);
        model.addAttribute("manager",manager);
        return "manager_update";
    }

    @PostMapping("/update")
    public String update(Manager manager){
        System.out.println("manager = " + manager);
managerService.updateById(manager);
        return "redirect:/admin/manager/list";
    }
    @GetMapping("/del/{id}")
    public String del(@PathVariable int id){
        managerService.removeById(id);
        return "redirect:/admin/manager/list";
    }
}


